package com.noble.glamour.ui.agora.act.voice.girlver;

import android.content.Context;
import android.os.SystemClock;
import android.view.View;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.TextView;

import com.noble.library.view.base.BasePanel;
import com.noble.glamour.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.ui.agora.model.AgoraConstant;
import com.noble.glamour.ui.agora.model.EngineConfig;
import com.noble.glamour.ui.agora.util.RtcTimerUtil;

/**
 * 女性任务版： 等待页面底部panel
 * <p>
 * Created by Su on 2017/6/15.
 */
public class RtcInitFootPanel extends BasePanel implements View.OnClickListener {
    private EngineConfig config;
    private Chronometer inviteCountUp;
    private ImageView btn_hangUp;
    private TextView tipsHangUp, tvInviteNum, tvChatPrice;

    private long cancelTime;        // 取消时间

    public RtcInitFootPanel(Context context) {
        super(context);
        setContentView(R.layout.f1_chat_init_foot_panel);
        initView();
    }

    private void initView() {
        config = ModuleMgr.getRtcEnginMgr().getEngineConfig();
        cancelTime = AgoraConstant.CHAT_TIME_AUTO_HUANGUP_GROUP;
        RtcTimerUtil.getInstance().startStatisInvTime();
        inviteCountUp = (Chronometer) findViewById(R.id.ch_inv_time);
        RtcTimerUtil.getInstance().addTimer(inviteCountUp);
        inviteCountUp.setBase(SystemClock.elapsedRealtime() - config.mInviteTime);
        inviteCountUp.start();

        tipsHangUp = (TextView) findViewById(R.id.tips_hang_up);
        tvInviteNum = (TextView) findViewById(R.id.tv_invited_num);
        tvChatPrice = (TextView) findViewById(R.id.chat_price);
        tvInviteNum.setText(getContext().getString(R.string.tip_chat_invite_total_num, config.mGroupInvNum));
        initHangUp();

        if (config.mGirlType == AgoraConstant.RTC_CHAT_SINGLE) {
            findViewById(R.id.send_num).setVisibility(View.GONE);
        }

        // 语音邀请
        if (config.mChatType == AgoraConstant.RTC_CHAT_VOICE) {
            cancelTime = AgoraConstant.CHAT_TIME_AUTO_HUANGUP_SINGLE;
            tipsHangUp.setText(getContext().getString(R.string.tip_hang_up_single));
            tvChatPrice.setText(getContext().getString(R.string.tip_chat_voice_price, config.mChatPrice));
            return;
        }

        // 视频邀请
        TextView tvWaiting = (TextView) findViewById(R.id.tv_waiting);
        tvWaiting.setText(getContext().getString(R.string.chat_waiting_for_answer_video));
        tvChatPrice.setText(getContext().getString(R.string.tip_chat_video_price, config.mChatPrice));
    }

    private void initHangUp() {
        btn_hangUp = (ImageView) findViewById(R.id.btn_invite_hangup);

        if (config.mChatType == AgoraConstant.RTC_CHAT_VIDEO) {
            if (config.mInviteTime / 1000 >= AgoraConstant.CHAT_TIME_AUTO_HUANGUP_GROUP) {
                resetHangUp();
            }
            return;
        }

        if (config.mChatType == AgoraConstant.RTC_CHAT_VOICE) {
            if (config.mInviteTime / 1000 >= AgoraConstant.CHAT_TIME_AUTO_HUANGUP_SINGLE)
                resetHangUp();
        }
    }

    private void resetHangUp() {
        btn_hangUp.setOnClickListener(this);
        btn_hangUp.setImageResource(R.drawable.f1_hang_up_enable);
        tipsHangUp.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_invite_hangup:
                reqVCGroupCancel();
                break;
        }
    }

    /**
     * 取消群发邀请
     */
    private void reqVCGroupCancel() {
        ModuleMgr.getRtcEnginMgr().reqVCGroupCancel(config.mInviteID, new RequestComplete() {
            @Override
            public void onRequestComplete(HttpResponse response) {
                getActivity().finish();
            }
        });
    }

    /**
     * 邀请人数
     */
    public void refreshInviteNum(int value) {
        if (config.mGirlType == AgoraConstant.RTC_CHAT_SINGLE) return;
        tvInviteNum.setText(getContext().getString(R.string.tip_chat_invite_total_num, (int) value));
    }

    /**
     * 邀请状态
     */
    public void refreshHangUp() {
        long totalSec = config.mInviteTime / 1000;
        if (totalSec == cancelTime)      // 达到指定计时
            resetHangUp();
    }
}